package com.taptech.doufu.ui.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.BrowseActivity;
import com.taptech.doufu.ui.activity.EasyBrowseActivity;
import com.taptech.doufu.bean.DFHomeBannerBean;
import com.taptech.doufu.bean.MarketNoticeBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.util.StartActivityUtil;
import com.taptech.doufu.util.TMAnalysis;

import java.util.List;

public class AutoScrollView extends LinearLayout
{
    private static final String TAG = "LILITH";
    private Context mContext;
    private ViewFlipper viewFlipper;
    private View scrollTitleView;
    private List<MarketNoticeBean> showForLeftList;
    private TextView tvLeftTag;

    public AutoScrollView(Context context)
    {
        super(context);
        mContext = context;
        init();
    }

    public AutoScrollView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        mContext = context;
        init();

    }

    public void updateScroolNotices(TextView tv,List<MarketNoticeBean> mList)
    {
        tvLeftTag=tv;
        viewFlipper.removeAllViews();
        if ((mList == null) || (mList.size() < 1))
        {
            return;
        }
        showForLeftList=mList;
        for (final MarketNoticeBean autoScrollViewBean : mList)
        {
            View scrollView = View.inflate(mContext, R.layout.list_item_autoscrollview, null);
            scrollView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    if ("1".equals(autoScrollViewBean.getNeed_login()) &&
                            !AccountService.getInstance().isLogin()) {
                        AccountService.getInstance().jumpToLogin();
                        return;
                    }
                    if ("0".equals(autoScrollViewBean.getObject_type())){
                        //跳转到浏览器
                        Intent intent = new Intent();
                        intent.setClass(mContext, BrowseActivity.class);
                        intent.putExtra(Constant.URL, autoScrollViewBean.getUrl());
                        intent.putExtra(Constant.TITLE, autoScrollViewBean.getMsg());
                        mContext.startActivity(intent);
                    }else{
                        DFHomeBannerBean bean = new DFHomeBannerBean();
                        bean.setObject_type(autoScrollViewBean.getObject_type());
                        bean.setTitle(autoScrollViewBean.getMsg());
                        bean.setUrl(autoScrollViewBean.getUrl());
                        bean.setId(autoScrollViewBean.getObject_id());
                        StartActivityUtil.bannerOnclick(mContext,bean);
                    }

                    TMAnalysis.event(mContext, "3_7_bookshelf_Advertisement");
                }
            });
            TextView tvType = (TextView) scrollView.findViewById(R.id.tv_home_type);
            tvType.setText(autoScrollViewBean.getMsgType());
            tvType.setVisibility(View.GONE);
            TextView tvText = (TextView) scrollView.findViewById(R.id.tv_home_text);
            tvText.setText(autoScrollViewBean.getMsg());
            viewFlipper.addView(scrollView);
            if((tvLeftTag!=null)&&(showForLeftList!=null)&&(viewFlipper!=null)){
                tvLeftTag.setText(showForLeftList.get(0).getMsgType());
            }
        }
        if ((mList != null) && (mList.size() > 1))
        {
            viewFlipper.startFlipping();
        }else if((mList != null) && (mList.size() == 1 )){
            try{
                viewFlipper.stopFlipping();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    private void init()
    {
        scrollTitleView = LayoutInflater.from(mContext).inflate(R.layout.list_item_autoscrollview, null);
        LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        addView(scrollTitleView, layoutParams);

        viewFlipper = (ViewFlipper) scrollTitleView.findViewById(R.id.flipper_home_autoscrollview);
        Animation tempAnimation=AnimationUtils.loadAnimation(mContext, R.anim.slide_in_up);
        viewFlipper.setInAnimation(tempAnimation);
        viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(mContext, R.anim.slide_out_up));

        tempAnimation.setAnimationListener(new Animation.AnimationListener()
        {
            @Override
            public void onAnimationStart(Animation animation)
            {

            }

            @Override
            public void onAnimationEnd(Animation animation)
            {
//                Log.e("tag","fuck"+viewFlipper.getDisplayedChild());
                if((tvLeftTag!=null)&&(showForLeftList!=null)&&(viewFlipper!=null)&&(viewFlipper.getDisplayedChild()<=showForLeftList.size())){
                    tvLeftTag.setText(showForLeftList.get(viewFlipper.getDisplayedChild()).getMsgType());
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation)
            {

            }
        });
    }

}
